<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="知识类别" prop="zsLb">
        <el-select v-model="queryParams.zsLb" placeholder="请选择知识类别" clearable>
          <el-option
            v-for="dict in dict.type.zs_lb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="知识类型" prop="zsLx">
        <el-select v-model="queryParams.zsLx" placeholder="请选择知识类型" clearable>
          <el-option
            v-for="dict in dict.type.zs_lx"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公文分类" prop="gwFl">
        <el-select v-model="queryParams.gwFl" placeholder="请选择公文分类" clearable>
          <el-option
            v-for="dict in dict.type.gw_fl"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="zsTitle">
        <el-input
          v-model="queryParams.zsTitle"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="fbrq">
        <el-input
          v-model="queryParams.fbrq"
          placeholder="请输入发布日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核日期" prop="shrq">
        <el-date-picker clearable
                        v-model="queryParams.shrq"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择审核日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="shzt">
        <el-input
          v-model="queryParams.shzt"
          placeholder="请输入审核状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类查看规则" prop="flckgz">
        <el-select v-model="queryParams.flckgz" placeholder="请选择分类查看规则" clearable>
          <el-option
            v-for="dict in dict.type.flckgz"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:zsShare:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:zsShare:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:zsShare:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:zsShare:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zsShareList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="true"/>
      <el-table-column label="组织机构" align="center" prop="deptId" />
      <el-table-column label="知识类别" align="center" prop="zsLb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zs_lb" :value="scope.row.zsLb"/>
        </template>
      </el-table-column>
      <el-table-column label="知识类型" align="center" prop="zsLx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zs_lx" :value="scope.row.zsLx"/>
        </template>
      </el-table-column>
      <el-table-column label="公文分类" align="center" prop="gwFl">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gw_fl" :value="scope.row.gwFl"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="zsTitle" />
      <el-table-column label="内容" align="center" prop="zsnr" />
      <el-table-column label="传阅人会签记录" align="center" prop="cyrhqjl" />
      <el-table-column label="传阅方式" align="center" prop="cyfs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cyfs" :value="scope.row.cyfs"/>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" align="center" prop="fbrq" />
      <el-table-column label="发布人" align="center" prop="fbr" />
      <el-table-column label="审核人" align="center" prop="shr" />
      <el-table-column label="审核日期" align="center" prop="shrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="shzt" />
      <el-table-column label="附件" align="center" prop="fujian" />
      <el-table-column label="分类查看规则" align="center" prop="flckgz">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flckgz" :value="scope.row.flckgz"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:zsShare:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:zsShare:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识共享对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="组织机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入组织机构" />
        </el-form-item>
        <el-form-item label="知识类别" prop="zsLb">
          <el-select v-model="form.zsLb" placeholder="请选择知识类别">
            <el-option
              v-for="dict in dict.type.zs_lb"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="知识类型" prop="zsLx">
          <el-select v-model="form.zsLx" placeholder="请选择知识类型">
            <el-option
              v-for="dict in dict.type.zs_lx"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公文分类" prop="gwFl">
          <el-select v-model="form.gwFl" placeholder="请选择公文分类">
            <el-option
              v-for="dict in dict.type.gw_fl"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="zsTitle">
          <el-input v-model="form.zsTitle" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="zsnr">
          <el-input v-model="form.zsnr" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="传阅人会签记录" prop="cyrhqjl">
          <el-input v-model="form.cyrhqjl" placeholder="请输入传阅人会签记录" />
        </el-form-item>
        <el-form-item label="传阅方式" prop="cyfs">
          <el-select v-model="form.cyfs" placeholder="请选择传阅方式">
            <el-option
              v-for="dict in dict.type.cyfs"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布日期" prop="fbrq">
          <el-input v-model="form.fbrq" placeholder="请输入发布日期" />
        </el-form-item>
        <el-form-item label="发布人" prop="fbr">
          <el-input v-model="form.fbr" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="审核人" prop="shr">
          <el-input v-model="form.shr" placeholder="请输入审核人" />
        </el-form-item>
        <el-form-item label="审核日期" prop="shrq">
          <el-date-picker clearable
                          v-model="form.shrq"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择审核日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="shzt">
          <el-input v-model="form.shzt" placeholder="请输入审核状态" />
        </el-form-item>
        <el-form-item label="附件" prop="fujian">
          <el-input v-model="form.fujian" placeholder="请输入附件" />
        </el-form-item>
        <el-form-item label="分类查看规则" prop="flckgz">
          <el-select v-model="form.flckgz" placeholder="请选择分类查看规则">
            <el-option
              v-for="dict in dict.type.flckgz"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listZsShare, getZsShare, delZsShare, addZsShare, updateZsShare } from "@/api/pm/zsShare";

  export default {
    name: "ZsShare",
    dicts: ['zs_lb', 'cyfs', 'flckgz', 'gw_fl', 'zs_lx'],
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 知识共享表格数据
        zsShareList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          zsLb: undefined,
          zsLx: undefined,
          gwFl: undefined,
          zsTitle: undefined,
          fbrq: undefined,
          shrq: undefined,
          shzt: undefined,
          flckgz: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          zsLb: [
            { required: true, message: "知识类别不能为空", trigger: "change" }
          ],
          zsLx: [
            { required: true, message: "知识类型不能为空", trigger: "change" }
          ],
          gwFl: [
            { required: true, message: "公文分类不能为空", trigger: "change" }
          ],
          zsTitle: [
            { required: true, message: "标题不能为空", trigger: "blur" }
          ],
          zsnr: [
            { required: true, message: "内容不能为空", trigger: "blur" }
          ],
          cyrhqjl: [
            { required: true, message: "传阅人会签记录不能为空", trigger: "blur" }
          ],
          cyfs: [
            { required: true, message: "传阅方式不能为空", trigger: "change" }
          ],
          fbrq: [
            { required: true, message: "发布日期不能为空", trigger: "blur" }
          ],
          fbr: [
            { required: true, message: "发布人不能为空", trigger: "blur" }
          ],
          shrq: [
            { required: true, message: "审核日期不能为空", trigger: "blur" }
          ],
          shzt: [
            { required: true, message: "审核状态不能为空", trigger: "blur" }
          ],
          fujian: [
            { required: true, message: "附件不能为空", trigger: "blur" }
          ],
          flckgz: [
            { required: true, message: "分类查看规则不能为空", trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询知识共享列表 */
      getList() {
        this.loading = true;
        listZsShare(this.queryParams).then(response => {
          this.zsShareList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          deptId: undefined,
          zsLb: undefined,
          zsLx: undefined,
          gwFl: undefined,
          zsTitle: undefined,
          zsnr: undefined,
          cyrhqjl: undefined,
          cyfs: undefined,
          fbrq: undefined,
          fbr: undefined,
          shr: undefined,
          shrq: undefined,
          shzt: undefined,
          fujian: undefined,
          flckgz: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加知识共享";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getZsShare(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改知识共享";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateZsShare(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addZsShare(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除知识共享编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delZsShare(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/zsShare/export', {
          ...this.queryParams
        }, `zsShare_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
